package org.szw.jpademo.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.szw.jpademo.dao.UserDao;
import org.szw.jpademo.entity.User;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

/**
 * @Description
 * @Author 申紫微
 * @Data 2020-06-05 17:05:49
 * @Version 1.0
 */
@Service
@Slf4j
public class UserService {

    @Autowired
    private UserDao userDao;

    @GetMapping("save")
    public boolean saveUser(User user){
        User save = userDao.save(user);
        return true;
    }

    @GetMapping("findByName")
    public User findByName(String name){
        User user = userDao.queryUserByNameV1(name);
        log.info("User===>{}",user.toString());
        return user;
    }

    public void findByPage(Pageable page){
        Specification<User> specification = new Specification<User>() {
            public Predicate toPredicate(Root<User> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
                return criteriaBuilder.or(criteriaBuilder.equal(root.get("name"),"111"),
                criteriaBuilder.equal(root.get("id"),8));
            }
        };


    }


}
